using Microsoft.AspNetCore.Mvc;
using MediPlatform.Read.Api.Models;
using MediPlatform.Read.Api.Services;
using MediPlatform.ErrorCode;

namespace MediPlatform.Read.Api.Controllers
{
    /// <summary>
    /// 退款申请控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class RefundApplicationController : ControllerBase
    {
        private readonly RefundApplicationService _refundApplicationService;
        private readonly ILogger<RefundApplicationController> _logger;

        public RefundApplicationController(
            RefundApplicationService refundApplicationService,
            ILogger<RefundApplicationController> logger)
        {
            _refundApplicationService = refundApplicationService;
            _logger = logger;
        }

        /// <summary>
        /// 获取退款申请分页列表
        /// </summary>
        /// <param name="request">查询请求</param>
        /// <returns>分页结果</returns>
        [HttpGet("list")]
        public async Task<ApiResult<RefundApplicationPagedResponse>> GetPagedList([FromQuery] RefundApplicationQueryRequest request)
        {
            try
            {
                _logger.LogInformation("获取退款申请列表，查询参数：{@Request}", request);

                var result = await _refundApplicationService.GetPagedListAsync(request);

                return ApiResult<RefundApplicationPagedResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取退款申请列表失败");
                return ApiResult<RefundApplicationPagedResponse>.Error("获取退款申请列表失败");
            }
        }

        /// <summary>
        /// 获取退款申请详情
        /// </summary>
        /// <param name="id">申请ID</param>
        /// <returns>详情信息</returns>
        [HttpGet("detail/{id}")]
        public async Task<ApiResult<RefundApplicationDetailResponse>> GetDetail(long id)
        {
            try
            {
                _logger.LogInformation("获取退款申请详情，ID：{Id}", id);

                var result = await _refundApplicationService.GetDetailAsync(id);

                if (result == null)
                {
                    return ApiResult<RefundApplicationDetailResponse>.Error("退款申请不存在");
                }

                return ApiResult<RefundApplicationDetailResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取退款申请详情失败，ID：{Id}", id);
                return ApiResult<RefundApplicationDetailResponse>.Error("获取退款申请详情失败");
            }
        }

        /// <summary>
        /// 获取退款申请统计信息
        /// </summary>
        /// <returns>统计信息</returns>
        [HttpGet("statistics")]
        public async Task<ApiResult<object>> GetStatistics()
        {
            try
            {
                _logger.LogInformation("获取退款申请统计信息");

                // 获取各状态的统计信息
                var totalCount = await _refundApplicationService.GetTotalCountAsync();
                var pendingCount = await _refundApplicationService.GetCountByStatusAsync(0);
                var processedCount = await _refundApplicationService.GetCountByStatusAsync(1);
                var rejectedCount = await _refundApplicationService.GetCountByStatusAsync(2);

                var statistics = new
                {
                    TotalCount = totalCount,
                    PendingCount = pendingCount,
                    ProcessedCount = processedCount,
                    RejectedCount = rejectedCount
                };

                return ApiResult<object>.Success(statistics);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取退款申请统计信息失败");
                return ApiResult<object>.Error("获取退款申请统计信息失败");
            }
        }

        /// <summary>
        /// 确认退款申请
        /// </summary>
        /// <param name="request">确认退款请求</param>
        /// <returns>处理结果</returns>
        [HttpPost("confirm")]
        public async Task<ApiResult<object>> ConfirmRefund([FromBody] ConfirmRefundRequest request)
        {
            try
            {
                _logger.LogInformation("确认退款申请，ID：{Id}，操作员：{OperatorId}", request.Id, request.OperatorId);

                // 验证请求参数
                if (request.Id <= 0)
                {
                    return ApiResult<object>.Error("退款申请ID无效");
                }

                if (request.OperatorId <= 0)
                {
                    return ApiResult<object>.Error("操作员ID无效");
                }

                // 调用服务处理
                var result = await _refundApplicationService.ConfirmRefundAsync(request);

                if (result)
                {
                    return ApiResult<object>.Success("退款申请确认成功");
                }
                else
                {
                    return ApiResult<object>.Error("退款申请确认失败，请检查申请状态");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "确认退款申请失败，ID：{Id}", request.Id);
                return ApiResult<object>.Error("确认退款申请失败");
            }
        }

        /// <summary>
        /// 拒绝退款申请
        /// </summary>
        /// <param name="request">拒绝退款请求</param>
        /// <returns>处理结果</returns>
        [HttpPost("reject")]
        public async Task<ApiResult<object>> RejectRefund([FromBody] RejectRefundRequest request)
        {
            try
            {
                _logger.LogInformation("拒绝退款申请，ID：{Id}，操作员：{OperatorId}", request.Id, request.OperatorId);

                // 验证请求参数
                if (request.Id <= 0)
                {
                    return ApiResult<object>.Error("退款申请ID无效");
                }

                if (request.OperatorId <= 0)
                {
                    return ApiResult<object>.Error("操作员ID无效");
                }

                // 调用服务处理
                var result = await _refundApplicationService.RejectRefundAsync(request);

                if (result)
                {
                    return ApiResult<object>.Success("退款申请拒绝成功");
                }
                else
                {
                    return ApiResult<object>.Error("退款申请拒绝失败，请检查申请状态");
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "拒绝退款申请失败，ID：{Id}", request.Id);
                return ApiResult<object>.Error("拒绝退款申请失败");
            }
        }


    }
} 